package com.kaishengit.action;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.interceptor.SessionAware;

import com.kaishengit.pojo.User;
import com.kaishengit.service.IndexService;
import com.opensymphony.xwork2.ActionSupport;

public class IndexAction extends ActionSupport implements SessionAware {

    private static final long serialVersionUID = 1L;
    private IndexService indexService;
    
    private String code;
    private User user;
    private Map<String, Object> session;

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
    
    public String login() {
        if (StringUtils.isEmpty(user.getEmail()) || StringUtils.isEmpty(user.getPassword())) {
            code = "102";
            return ERROR;
        } else {
            User u = indexService.login(user);
            if (u != null) {
                if (!u.isEnable()) {
                    code = "103";
                    return ERROR;
                } else if (u.getPassword().equals(user.getPassword())) {
                    session.put("user", u);
                    return SUCCESS;
                } else {
                    code = "101";
                    return ERROR;
                }
            } else {
                code = "101";
                return ERROR; 
            }
        }
    }

    //set get
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void setSession(Map<String, Object> session) {
        this.session = session;
    }

    public IndexService getIndexService() {
        return indexService;
    }

    public void setIndexService(IndexService indexService) {
        this.indexService = indexService;
    }
}
